package com.xwb.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xwb.mapper.BookMapper;
import com.xwb.mapper.LifeMapper;
import com.xwb.pojo.Book;
import com.xwb.pojo.Life;
import com.xwb.pojo.Review;

@CrossOrigin
@Controller
public class BookController {
	@Autowired
	private BookMapper bookMapper;
	
	/*
	 * 遍历列表
	 */
	@RequestMapping("/booklist")
	@ResponseBody
	public Map<String, Object> List() {
		Map<String, Object> map = new HashMap<String, Object>();
		ArrayList<Book> book = bookMapper.list();
		try {
			map.put("book", book);
		} catch (Exception e) {
			e.printStackTrace();
			map.clear();
		}
		System.out.println("推荐书籍列表遍历成功");
		return map;		
		
	}


	/*
	 * 根据id显示详细内容
	 */
	@RequestMapping("/bookdetail")
	@ResponseBody
	public Map<String, Object> getObj(@RequestBody Map<String, Integer> requestMap) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			map.put("book", bookMapper.findByid(requestMap.get("bid")));
		} catch (Exception e) {
			e.printStackTrace();
			map.clear();
		}
		System.out.println(requestMap.get("bid")+"号内容显示成功");
		return map;
	}

	
	/*
	 * 点赞
	 */
	@RequestMapping("/bookwant")
	@ResponseBody
	public void bookwant(@RequestBody Map<String, Integer> requestMap) {
		bookMapper.updatawant(requestMap.get("bid"), requestMap.get("bwant"));
		System.out.println("点赞成功");
	}
	
	
	/*
	 * 评论
	 */
	@RequestMapping("/brsend")
	@ResponseBody
	public void rsend(@RequestBody Review review) {
		bookMapper.addreview(review);
		System.out.println("评论成功");
	}
	
	/*
	 * 遍历评论
	 */
	@RequestMapping("/brlist")
	@ResponseBody
	public Map<String, Object> rlist(@RequestBody Map<String, Integer> requestMap) {
		Map<String, Object> map = new HashMap<String, Object>();
		ArrayList<Review> review = bookMapper.findreviewByid(requestMap.get("bid"));
		try {
			map.put("review", review);
		} catch (Exception e) {
			e.printStackTrace();
			map.clear();
		}
		System.out.println("书籍推荐评论遍历成功");
		return map;			
	}
}
